package com.hyyt.shopjsp.panicBuying.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.service.IShopHomePanicRecommendService;
import com.hyyt.shopjsp.panicBuying.pojo.ShopPanicBuying;
import com.hyyt.shopjsp.panicBuying.service.IShopPanicBuyingService;
import com.hyyt.shopjsp.product.pojo.ShopProductInfo;
import com.hyyt.shopjsp.product.service.IShopProductInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/20 0024下午 1:20.
 */

@Controller
@RequestMapping("/back/panicBuying")
public class ShopPanicBuyingController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopPanicBuyingController.class);
	private String basePath = "/back/panicBuying/";
	@Resource
	private IShopPanicBuyingService shopPanicBuyingService; //抢购商品service接口
	@Resource
	private IShopProductInfoService shopProductInfoService; //商品service接口
	@Resource
	private IShopHomePanicRecommendService shopHomePanicRecommendService; //抢购推荐记录service

	/**
	 * 进入抢购商品管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoPanicBuyingPage")
	public String gotoPanicBuyingPage(){
		return basePath+"panicBuyingList";
	}

	/**
	 * 查询所有抢购商品
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listPanicBuying",method=RequestMethod.POST)
	@ResponseBody
	public void listPanicBuying(String panicTitle,String isPublish,String isOver,String isShow, Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(panicTitle)){
			params.put("panicTitle",panicTitle.trim());
		}
		if(Utils.objectIsNotEmpty(isPublish)){
			params.put("isPublish",isPublish);
		}
		if(Utils.objectIsNotEmpty(isOver)){
			params.put("isOver",isOver);
		}
		if(Utils.objectIsNotEmpty(isShow)){
			params.put("isShow",isShow);
		}
		params.put("sortColumns","panicId DESC");
        int totalRecordCount = shopPanicBuyingService.countShopPanicBuying(params);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<ShopPanicBuying> panicBuyingList = shopPanicBuyingService.queryShopPanicBuyingList(params, page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", panicBuyingList);// rows键 存放每页记录 list
		ResultMsg.create(params, response,true);  //把内容发送到页面
	}

	/**
	 * 查询可以参与抢购商品列表
	 * 暂时需要满足两个状态
	 * 1、商品已经上架 isPutSale = 2
	 * 2、商品审核已通过 isPass = 1
	 */
	@RequestMapping(value="listProductInfo",method = RequestMethod.POST)
	@ResponseBody
	public void listProductInfo(String productFullName,String productNo,Page page, HttpServletResponse response){
		Map<String,Object> params=new HashMap<>();
		if(Utils.objectIsNotEmpty(productFullName)){
			params.put("productFullName",productFullName);
		}
		if(Utils.objectIsNotEmpty(productNo)){
			params.put("productNo",productNo);
		}
		int totalRecordCount =shopProductInfoService.queryProductInfoCount(params);
		List<ShopProductInfo> productInfoList = shopProductInfoService.queryProductInfoList(params,page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", productInfoList);// rows键 存放每页记录 list
		ResultMsg.create(params, response,true);  //把内容发送到页面
	}

	/**
	 * 删除抢购商品
	 * @param panicId
	 * @param isPublish
	 * @param isOver
	 * @return
	 */
    @RequestMapping(value = "deletePanicBuying", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg deletePanicBuying(Integer panicId,Integer isPublish,Integer isOver){
	    boolean flag=false;
		if((isPublish==0||isOver==1)&& Utils.objectIsNotEmpty(panicId)){//只有未发布和已经结束的抢购才可以删除
			shopPanicBuyingService.deleteShopPanicBuying(panicId);
			flag=true;
		}
		return ResultMsg.create(flag);
	}

    /**
     * 关闭结束抢购
     * @param panicId
     * @param isPublish
     * @param isOver
     * @return
     */
    @RequestMapping(value="closePanicBuying",method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg closePanicBuying(Integer panicId,Integer isPublish,Integer isOver){
	    Map<String,Object> params=new HashMap<>();
        boolean flag=false;
        if((isPublish==1&&isOver==0)&& Utils.objectIsNotEmpty(panicId)){ //只有正在抢购的可以关闭结束
            params.put("isOver",1);
            params.put("panicId",panicId);
            shopPanicBuyingService.updatePanicBuyingByParams(params);
            flag=true;
        }
	    return ResultMsg.create(flag);
    }

    /**
     * 开启抢购发布状态
     * @param panicId
     * @return
     */
    @RequestMapping(value = "updatePublish",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg updatePublish(Integer panicId){
        String msg="";
        Map<String,Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(panicId)){
            ShopPanicBuying shopPanicBuying = shopPanicBuyingService.loadShopPanicBuying(panicId);
            if(Utils.objectIsNotEmpty(shopPanicBuying)){
                Integer productId = shopPanicBuying.getProductId();
                params.put("productId",productId);
                int count = shopPanicBuyingService.countShopPanicBuying(params);
                params.clear();
                if (count == 1) {
                    params.put("updateTime",new Timestamp(System.currentTimeMillis()));
                    params.put("panicId",panicId);
                    shopPanicBuyingService.updatePanicBuyingByParams(params);
                    msg="true";
                }else{
                    msg="false1";
                }
            }else{
                msg="false";
            }
        }else{
            msg="false";
        }
        return ResultMsg.create(true,msg);
    }

	/**
	 * 设置推荐栏显示
	 * @param panicId
	 * @return
	 */
	@RequestMapping(value="updateIndexShow",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg updateIndexShow(Integer panicId){
    	Map<String,Object> params=new HashMap<>();
    	boolean flag=false;
		if(Utils.objectIsNotEmpty(panicId)){
			ShopPanicBuying shopPanicBuying = shopPanicBuyingService.loadShopPanicBuying(panicId);
			if(Utils.objectIsNotEmpty(shopPanicBuying)){
				params.put("isShow",1);
				params.put("panicId",panicId);
				params.put("isShowUpdateTime",new Timestamp(System.currentTimeMillis()));
				shopPanicBuyingService.updatePanicBuyingByParams(params);
				flag=true;
			}
		}
    	return ResultMsg.create(flag);
	}

	/**
	 * 添加或修改抢购
	 * @param shopPanicBuying
	 * @return
	 */
	@RequestMapping(value = "addPanicBuying",method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg addPanicBuying(ShopPanicBuying shopPanicBuying){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(shopPanicBuying)){
			if(Utils.objectIsNotEmpty(shopPanicBuying.getPanicId())){
				ShopPanicBuying shopPanicBuyingNew = shopPanicBuyingService.loadShopPanicBuying(shopPanicBuying.getPanicId());
				shopPanicBuyingNew.setReducePrice(shopPanicBuying.getOriginalPrice().subtract(shopPanicBuying.getPanicPrice()));
				shopPanicBuyingNew.setUpdateTime(new Timestamp(System.currentTimeMillis()));
				shopPanicBuyingNew.setIsShow(0); //是否显示
				shopPanicBuyingNew.setIsPublish(0); //是否发布
				shopPanicBuyingNew.setIsOver(0);//是否关闭
				shopPanicBuyingNew.setBuyNum(0); //已购个数
				shopPanicBuyingNew.setPanicTitle(shopPanicBuying.getPanicTitle()); //抢购标题
				shopPanicBuyingNew.setPanicNum(shopPanicBuying.getPanicNum()); //抢购数量
				shopPanicBuyingNew.setPanicPrice(shopPanicBuying.getPanicPrice()); //抢购价格
				shopPanicBuyingNew.setStartTime(shopPanicBuying.getStartTime()); //开始时间
				shopPanicBuyingNew.setActivityTime(shopPanicBuying.getActivityTime()); //活动市场
				shopPanicBuyingNew.setLogoImg(shopPanicBuying.getLogoImg()); //抢购图片
				shopPanicBuyingService.updateShopPanicBuying(shopPanicBuyingNew); //修改抢购操作
				flag=true;
			}else{
				shopPanicBuying.setIsPublish(0); //是否发布
				shopPanicBuying.setIsOver(0); //是否结束
				shopPanicBuying.setBuyNum(0); //已购个数
				shopPanicBuying.setIsShow(0); //是否显示
				shopPanicBuying.setReducePrice(shopPanicBuying.getOriginalPrice().subtract(shopPanicBuying.getPanicPrice())); //立省金额
				shopPanicBuying.setCreateTime(new Timestamp(System.currentTimeMillis())); //创建时间
				shopPanicBuying.setUpdateTime(new Timestamp(System.currentTimeMillis())); //修改时间
				shopPanicBuyingService.saveShopPanicBuying(shopPanicBuying); //添加抢购操作
				flag=true;
			}
		}
    	return ResultMsg.create(flag);
	}

	/**
	 * 根据抢购Id查询抢购对象
	 * @param panicId 抢购Id
	 * @return
	 */
	@RequestMapping(value="getPanicBying",method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getPanicBying(Integer panicId){
		boolean flag=false;
		if(Utils.objectIsNotEmpty(panicId)){
			ShopPanicBuying shopPanicBuying = shopPanicBuyingService.loadShopPanicBuying(panicId);
			if(Utils.objectIsNotEmpty(shopPanicBuying)){
				return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopPanicBuying));
			}
		}
		return ResultMsg.create(flag);
	}


	/**
	 *  查看商品抢购记录
	 * @param panicTitle
	 * @param productFullName
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listPanicBuyingRecord",method = RequestMethod.POST)
	@ResponseBody
	public void listPanicBuyingRecord(String panicTitle,String productFullName,Page page,HttpServletResponse response){
		Map<String,Object> params=new HashMap<>();
		if(Utils.objectIsNotEmpty(panicTitle)){
			params.put("panicTitle",panicTitle);
		}
		if(Utils.objectIsNotEmpty(productFullName)){
			params.put("productFullName",productFullName);
		}
		int totalRecordCount = shopHomePanicRecommendService.countHomePanicRecommendProduct(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<Map<String,Object>> panicBuyingRecordList = shopHomePanicRecommendService.queryHomePanicRecommendProduct(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", panicBuyingRecordList);// rows键 存放每页记录 list
		ResultMsg.create(params, response,true);  //把内容发送到页面
	}
}
